<?php

namespace app\common\service;

use think\App;
use think\facade\Config;
class SystemService
{
    /**
     * 获取当前应用的内存占用、PHP环境、操作系统和ThinkPHP框架信息。
     *
     * @return array
     */
    public static function getSystemInfo(): array
    {
        return [
            'memory' => self::getMemoryInfo(),
            'php' => self::getPhpInfo(),
            'os' => self::getOsInfo(),
            'thinkphp' => self::getThinkPHPAppInfo(),
        ];
    }

    /**
     * 获取内存使用信息。
     *
     * @return array
     */
    protected static function getMemoryInfo(): array
    {
        return [
            'current_usage' => memory_get_usage(true), // 实际分配的内存
            'peak_usage' => memory_get_peak_usage(true), // 实际分配的内存峰值
        ];
    }

    /**
     * 获取PHP环境信息。
     *
     * @return array
     */
    protected static function getPhpInfo(): array
    {
        $serverInfo = [];
        $keys = ['SERVER_SOFTWARE', 'DOCUMENT_ROOT', 'REQUEST_URI', 'REMOTE_ADDR', 'SERVER_NAME', 'SERVER_PORT'];
        foreach ($keys as $key) {
            if (isset($_SERVER[$key])) {
                $serverInfo[$key] = $_SERVER[$key];
            }
        }

        $envInfo = [];
        // 过滤敏感环境变量，只显示部分或不显示敏感信息
        foreach ($_ENV as $key => $value) {
            if (!preg_match('/(password|secret|key|token|db_)/i', $key)) {
                $envInfo[$key] = $value;
            }
        }

        return [
            'version' => PHP_VERSION,
            'sapi' => php_sapi_name(),
            'is_cli' => PHP_SAPI === 'cli',
            'server' => $serverInfo,
            'environment_variables' => $envInfo,
        ];
    }

    /**
     * 获取操作系统信息。
     *
     * @return array
     */
    protected static function getOsInfo(): array
    {
        return [
            'name' => php_uname('s'),
            'hostname' => gethostname(),
            'version' => php_uname('v'),
            'architecture' => php_uname('m'),
        ];
    }

    /**
     * 获取ThinkPHP 8应用信息。
     *
     * @return array
     */
    protected static function getThinkPHPAppInfo(): array
    {
        $app = App::getInstance();

        // 获取部分关键配置，并过滤敏感信息
        $config = Config::get();
        $filteredConfig = [];
        foreach ($config as $key => $value) {
            if (is_array($value)) {
                // 递归过滤数组中的敏感信息，这里只做简单过滤
                if ($key === 'database' && isset($value['connections'])) {
                    $filteredConfig[$key] = ['connections' => '***filtered***'];
                } elseif ($key === 'cache' && isset($value['stores'])) {
                    $filteredConfig[$key] = ['stores' => '***filtered***'];
                } else {
                    $filteredConfig[$key] = '***filtered_array***'; // 对于复杂数组，简单标记
                }
            } elseif (!preg_match('/(password|secret|key|token|db_)/i', $key)) {
                $filteredConfig[$key] = $value;
            }
        }

        return [
            'version' => App::VERSION,
            'debug_mode' => $app->isDebug(),
            'root_path' => $app->getRootPath(),
            'app_path' => $app->getAppPath(),
            'runtime_path' => $app->getRuntimePath(),
            'config' => $filteredConfig,
        ];
    }


}